﻿using _01_Tower4._0.GameFrame;
using _01_Tower4._0.GameFrame.Manager;
using _01_Tower4._0.GameFrame.System;
using _01_Tower4._0.Interface;
using _01_Tower4._0.Scene;
using _01_Tower4._0.Skills;
using _01_Tower4._0.Tool;

namespace _01_Tower4._0.CharacterBase
{
    enum Direction
    {
        Up, Down, Left, Right
    }

    internal class Character : GameObject, IAttack
    {
        // 如果把成员变量公开 public
        // 不安全 因为外界能随意改动 破坏了类的封装性
        // 希望外界能获取 但不能修改
        public string? Name { get; }
        public static bool IsDialoging { get; set; }
        public static bool IsCombating { get; set; }
        public int Hp { get; set; }
        public int MaxHp { get; set; }
        public int Mp { get; set; }
        public int MaxMp { get; set; }
        public int Atk { get; set; }
        public int Def { get; set; }
        public bool IsDead { get; set; }


        protected Map map;
        protected string display;
        Random rand;
        ToolMenu toolMenu;
        ConsoleColor color;

        protected Character(string name, int hp, int maxHp, int mp, int maxMp, int atk, int def, Pos pos, Map map, string display, ConsoleColor color)
        {
            Name = name;
            Hp = hp;
            MaxHp = maxHp;
            Mp = mp;
            MaxMp = maxMp;
            Atk = atk;
            Def = def;
            Pos = pos;
            this.map = map;
            this.display = display;
            IsDead = false;
            IsDialoging = false;
            IsCombating = false;
            rand = new Random();
            this.color = color;
        }

        public Character(string name, int hp, int maxHp, int mp, int maxMp, int atk, int def, Pos pos, Map map, string display)
        {
            Name = name;
            Hp = hp;
            MaxHp = maxHp;
            Mp = mp;
            MaxMp = maxMp;
            Atk = atk;
            Def = def;
            Pos = pos;
            this.map = map;
            this.display = display;
        }

        public virtual void Init(ToolMenu toolMenu)
        {

        }

        public virtual void Render()
        {
            Utility.MyPrint(display, Pos.x + map.offsetX, Pos.y + map.offsetY,color);
        }

        public void Attack(IAttack target)
        {
            if (Atk > target.Def)
            {
                InfoSystem.Instance.AddInfo($"{Name}攻击了{((Character)target).Name}!", ConsoleColor.Green);
                target.TakeDamage(Atk - target.Def);
            }
            else
            {
                InfoSystem.Instance.AddInfo($"{Name}攻击了{((Character)target).Name},攻击无效!", ConsoleColor.DarkGray);
            }
        }

        public void TakeDamage(int damage)
        {
            InfoSystem.Instance.AddInfo($"{Name}受到了{damage}点伤害!", ConsoleColor.Yellow);
            Hp -= damage;
            JudgingDeath();
        }

        public void JudgingDeath()
        {

            if (Hp < 0) Hp = 0;
            // 死亡判定
            IsDead = Hp == 0;
            if (IsDead)
            {
                InfoSystem.Instance.AddInfo($"{Name}死亡!", ConsoleColor.Red);
                if (Name == "玩家")
                {
                    EventSystem.Instance.InvokeEvent("玩家死亡");
                    SceneManager.Instance.ChangeSceneByName("SceneMenu");
                }
                if (Name == "史莱姆")
                {
                    InfoSystem.Instance.AddInfo("获得史莱姆凝胶", ConsoleColor.Blue);
                    EventSystem.Instance.InvokeEvent("史莱姆凝胶");
                }
                if (Name == "哥布林")
                {
                    InfoSystem.Instance.AddInfo("获得哥布林长矛", ConsoleColor.Blue);
                    EventSystem.Instance.InvokeEvent("哥布林长矛");
                }
                if (Name == "魔王")
                {
                    SceneManager.Instance.ChangeSceneByName("SceneMenu");
                }
                OnDead();
            }
        }

        public virtual void OnDead()
        {
            // 告诉战斗系统 战斗结束
            EventSystem.Instance.InvokeEvent("战斗结束");
        }

        public void Skill(IAttack target)
        {
            if (SceneMap1.csTarget == "魔王")
            {
                Mp -= 20;
                if (Mp < 0)
                {
                    Mp = 0;
                    InfoSystem.Instance.AddInfo($"{Name}使用了技能<致命打击>", ConsoleColor.Red);
                    InfoSystem.Instance.AddInfo($"{Name}法力值不足", ConsoleColor.Red);
                    return;
                }
                InfoSystem.Instance.AddInfo($"{Name}使用了技能<致命打击>", ConsoleColor.Red);
                InfoSystem.Instance.AddInfo($"{Name}对{((Character)target).Name}造成了20伤害");
                target.TakeDamage(20);
            }
            if (SceneMap1.csTarget == "史莱姆")
            {
                Mp -= 10;
                if (Mp < 0)
                {
                    Mp = 0;
                    InfoSystem.Instance.AddInfo($"{Name}使用了技能<以柔克刚>", ConsoleColor.Blue);
                    InfoSystem.Instance.AddInfo($"{Name}法力值不足", ConsoleColor.Blue);
                    return;
                }
                InfoSystem.Instance.AddInfo($"{Name}使用了技能<以柔克刚>", ConsoleColor.Blue);
                InfoSystem.Instance.AddInfo($"{Name}防御力上升<防御力+1>");
                Def += 1;
            }
            if (SceneMap1.csTarget == "哥布林")
            {
                Mp -= 10;
                if (Mp < 0)
                {
                    Mp = 0;
                    InfoSystem.Instance.AddInfo($"{Name}使用了技能<振奋>", ConsoleColor.Green);
                    InfoSystem.Instance.AddInfo($"{Name}法力值不足", ConsoleColor.Green);
                    return;
                }
                InfoSystem.Instance.AddInfo($"{Name}使用了技能<振奋>", ConsoleColor.Green);
                InfoSystem.Instance.AddInfo($"{Name}攻击力上升<攻击力+1>");
                Atk += 1;
            }
            target.JudgingDeath();
        }

        public void Item(IAttack target)
        {
            InfoSystem.Instance.AddInfo($"item");
            //InfoSystem.Instance.AddInfo($"{Name}对{((Character)target).Name}使用了道具", ConsoleColor.Yellow);
            //target.TakeDamage(5);
        }

        public void Escape()
        {
            int i = rand.Next(100);
            if (i > 70)
            {
                InfoSystem.Instance.AddInfo($"{Name}逃跑成功", ConsoleColor.Green);
                // 告诉战斗系统 战斗结束
                EventSystem.Instance.InvokeEvent("战斗结束");
            }
            else
            {
                InfoSystem.Instance.AddInfo($"{Name}逃跑失败", ConsoleColor.Red);
            }
        }
    }
}